package webdavclient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.Map;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.StatusLine;

import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.commons.json.JsonUtil;
import org.apache.jackrabbit.webdav.client.methods.CopyMethod;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.attilax.net.HttpClientUtil4_0_1;
import com.attilax.net.urlUtils;
import com.google.common.collect.Maps;

//  /webdavclient/src/webdavclient/webdavclientUtil.java
public class webdavclientUtil implements cmsApi {

	public static void main(String[] args) throws HttpException, IOException {
		// jackrabbit ver 2.6.10
		HttpClient HttpClient1 = new HttpClient(); // HttpClient 3.1 jar
		Credentials creds = new UsernamePasswordCredentials("admin", "password");
		// client.getState().setCredentials(AuthScope.ANY, creds);

		// MKCOL method mk dir foler
		DavMethod mkCol = new MkColMethod("http://192.168.1.77:1316/webdavapp/webdavurl/tests67");
		HttpClient1.executeMethod(mkCol);
		int statusCode = mkCol.getStatusCode();
		String statusText = mkCol.getStatusText();
		StatusLine statusLine = mkCol.getStatusLine();
		System.out.println("d");
		String rar = "C:\\d\\dockbase\\jdk1.8.0_131.rar";
		URI uri = new File(rar).toURI();
		String destinationUri = "http://192.168.1.77:1316/webdavapp/webdavurl/tests67/jdk1.8.0_131.rar.zip";
		// copyMethod������һ����Դ�ӷ������ϵ�һ��λ�õ���һ��λ��
		// CopyMethod CopyMethod1= new CopyMethod(uri.toString(),
		// destinationUri, true); // unsupported protocol: 'file'

	//	upload(HttpClient1, rar, destinationUri);
		System.out.println("d2");

	}

	@Test
	public void test_upload() throws FileNotFoundException, HttpException, IOException {
		String source = "C:\\Users\\attilax\\Documents\\s6 doc compc\\Atitit    Ӧ�ó����������ܽ� v2 s66.docx";
		String destinationUri = "http://192.168.1.77:1316/webdavapp/webdavurl/tests67/aa�������ܽ�.docx";
		destinationUri=urlUtils.encodeURI (destinationUri);
		PutMethod putMethod1=	webdavclientUtil.upload(source, destinationUri);
		Map m=Maps.newLinkedHashMap();
		m.put("statusCode", putMethod1.getStatusCode());
		m.put("statusText", putMethod1.getStatusText());
		m.put("StatusLine", putMethod1.getStatusLine());
		System.out.println( JSON.toJSONString(m));
		System.out.println("--f");
	}
	
	
	@Test
	public void test_diwn() throws FileNotFoundException, HttpException, IOException {
	//	String source = "C:\\Users\\attilax\\Documents\\s6 doc compc\\Atitit     s66.docx";
	//	String destinationUri = "http://192.168.1.77:1316/webdavapp/webdavurl/tests67/aa .docx";
		String destinationUri = "http://192.168.1.77:1316/webdavapp/webdavurl/tests67/jdk1.8.0_131.rar.zip";
		
		down( destinationUri,"C:\\tmps67\\jdk1.8.0_131.rar.zip");
	 
		System.out.println("--f");
	}
	public static void down( String url,String outputstream1File) throws IOException
	{
		url=urlUtils.encodeURI (url);
		FileUtils.forceMkdir(new File(outputstream1File).getParentFile());
		HttpClientUtil4_0_1.httpget(url, outputstream1File);
	}
	public static PutMethod upload(String source, String destinationUri)

	throws FileNotFoundException, IOException, HttpException {
		
		destinationUri=urlUtils.encodeURI (destinationUri);

		HttpClient HttpClient1 = new HttpClient(); // HttpClient 3.1 jar
		PutMethod putMethod1 = new PutMethod(destinationUri.toString());
		RequestEntity requestEntity = new InputStreamRequestEntity(new FileInputStream(source));
		putMethod1.setRequestEntity(requestEntity);
		HttpClient1.executeMethod(putMethod1);
		int statusCode = putMethod1.getStatusCode();
		String statusText = putMethod1.getStatusText();
		StatusLine statusLine_CopyMethod1 = putMethod1.getStatusLine();
	
		return putMethod1; 
	}

}
